<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */
class X_Data_Adapter_Pdo extends PDO implements X_Data_Adapter_Interface
{
    /**
     * connection singelton
     *
     * @var PDO
     */
    protected $_oPdo;
    /**
     * query to be executed
     * @var string
     */
    protected $_sQuery;
    /**
     * instance singelton
     *
     * @var X_Data_Adapter_Pdo
     */
    static protected $_oInstance;
    /**
     * gets a static instance of self
     * 
     * @param string $sConnectionString
     * @param string $sUser
     * @param string $sPass
     * @param array $aOptions
     */
    static public function getInstance($sConnectionString, $sUser = null, $sPass = null, Array $aOptions = array(PDO::ATTR_PERSISTENT => true))
    {
        if (!self::$_oInstance)
        {
            $sClass = __CLASS__;
            self::$_oInstance = new $sClass($sConnectionString, $sUser, $sPass, $aOptions);
        }
        
        return self::$_oInstance;
    }
    
    /**
     * @see X_Data_Adapter_Interface::bind()
     *
     * @param string $sTokenName
     * @param string $sValue
     */
    public function bind($sTokenName, $sValue)
    {    //TODO - Insert your code here
    }
    
    /**
     * @see X_Data_Adapter_Interface::bindArray()
     *
     * @param array $aBindArray
     * @param bool $bReset
     */
    public function bindArray(Array $aBindArray, $bReset)
    {    //TODO - Insert your code here
    }
    
    /**
     * @see X_Data_Adapter_Interface::commit()
     *
     */
    public function commit()
    {    
    	$this->_oPdo->beginTransaction();
    }
    
    /**
     * @see X_Data_Adapter_Interface::countAlteredRows()
     *
     */
    public function countAlteredRows()
    {    //TODO - Insert your code here
    }
    
    /**
     * @see X_Data_Adapter_Interface::countRetrievedRows()
     *
     */
    public function countRetrievedRows()
    {    //TODO - Insert your code here
    }
    
	/**
     * @see X_Data_Adapter_Interface::getBindTokens()
     * 
     * @param string $sStatement
     */
    public function query($sStatement = null)
    {
    	if (empty($sStatement))
    	{
    		$sStatement = $this->_sQuery;
    	}
    	
    	$this->_oStatement = parent::query($sStatement);
    	// @todo wrap the PDOStatement object with an interfaced Statement/Result class
    	return $this->_oStatement;
    }
    
    /**
     * @see X_Data_Adapter_Interface::execute()
     *
     */
    public function execute()
    {    
    	// @todo check to see if there were binds then use prepare and execute
    	$this->_oStatement = $this->exec($this->_sQuery);
    	// @todo wrap the PDOStatement object with an interfaced Statement/Result class
    	return $this->_oStatement;
    }
    
    /**
     * @see X_Data_Adapter_Interface::getBindTokens()
     *
     * @return array
     */
    public function getBindTokens()
    {    //TODO - Insert your code here
    }
    
    /**
     * @see X_Data_Adapter_Interface::getLastInsertId()
     *
     */
    public function getLastInsertId($sName = null)
    {    
    	$this->lastInsertId($sName);
    }
    
    /**
     * @see X_Data_Adapter_Interface::getResult()
     *
     * @param string $sIndexName
     */
    public function getResult($sIndexName)
    {
    	return $this->_oStatementfetchAll(PDO::FETCH_ASSOC);
    }
    
    /**
     * @see X_Data_Adapter_Interface::getResultAssoc()
     *
     * @param string $sIndexName
     */
    public function getResultAssoc($sIndexName)
    {    
    	// @todo set the first level array index using the named column
    	return $this->_oStatement->fetchAll(PDO::FETCH_ASSOC);
    }
    
    /**
     * @see X_Data_Adapter_Interface::getResultObjectArray()
     *
     * @param string $sClassName
     * @param string $sIndexName
     */
    public function getResultObjectArray($sClassName = 'X_Data_Record', $sIndexName = null)
    {
    	if (empty($this->_oStatement))
    	{
    		$this->query();
    	}
    	// @todo set the first level array index using the named column
    	$this->_oStatement->setFetchMode(PDO::FETCH_CLASS, $sClassName);
    	return $this->_oStatement->fetchAll();
    }
    
    /**
     * @see X_Data_Adapter_Interface::limit()
     *
     * @param int $iStart
     * @param int $iCount
     */
    public function limit($iStart, $iCount)
    {    //TODO - Insert your code here
    }
        
    /**
     * @see X_Data_Adapter_Interface::setQuery()
     *
     * @param string $sString
     */
    public function setQuery($sString)
    {    
    	$this->_sQuery = $sString;
    }
    
    /**
     * @see X_Data_Adapter_Interface::setReturnMode()
     *
     * @param PDO_FETCH $iMode
     * @param unknown_type $xParam1
     * @param unknown_type $xParam2
     * @return bool
     */
    public function setReturnMode($iMode, $xParam1 = null, $xParam2 = null)
    {    //TODO - Insert your code here
    }
        
    static public function abbreviateTableName($sTableName)
	{
	    $aName = explode('_', $sTableName);
	    $sAbrv = '';

	    foreach ($aName as $sPiece)
	    {
	        if (!empty($sPiece))
	        {
	            $sAbrv .= $sPiece[0];
	        }
	    }

	    return $sAbrv;
	}

    
}